<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="id" prop="id">
            <el-input v-model="queryParams.id" placeholder="请输入id" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="编号" prop="tableId">
            <el-input v-model="queryParams.tableId" placeholder="请输入编号" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="数据源名称" prop="dataName">
            <el-input v-model="queryParams.dataName" placeholder="请输入数据源名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="表名称" prop="tableName">
            <el-input v-model="queryParams.tableName" placeholder="请输入表名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="表描述" prop="tableComment">
            <el-input v-model="queryParams.tableComment" placeholder="请输入表描述" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="关联子表的表名" prop="subTableName">
            <el-input v-model="queryParams.subTableName" placeholder="请输入关联子表的表名" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="子表关联的外键名" prop="subTableFkName">
            <el-input v-model="queryParams.subTableFkName" placeholder="请输入子表关联的外键名" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="实体类名称" prop="className">
            <el-input v-model="queryParams.className" placeholder="请输入实体类名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="使用的模板" prop="tplCategory">
            <el-input v-model="queryParams.tplCategory" placeholder="请输入使用的模板" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="生成包路径" prop="packageName">
            <el-input v-model="queryParams.packageName" placeholder="请输入生成包路径" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="生成模块名" prop="moduleName">
            <el-input v-model="queryParams.moduleName" placeholder="请输入生成模块名" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="生成业务名" prop="businessName">
            <el-input v-model="queryParams.businessName" placeholder="请输入生成业务名" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="生成功能名" prop="functionName">
            <el-input v-model="queryParams.functionName" placeholder="请输入生成功能名" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="生成功能作者" prop="functionAuthor">
            <el-input v-model="queryParams.functionAuthor" placeholder="请输入生成功能作者" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="业务名称" prop="sqlName">
            <el-input v-model="queryParams.sqlName" placeholder="请输入业务名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="生成路径" prop="genPath">
            <el-input v-model="queryParams.genPath" placeholder="请输入生成路径" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="其它生成选项" prop="options">
            <el-input v-model="queryParams.options" placeholder="请输入其它生成选项" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['srv:sqlGenTable:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['srv:sqlGenTable:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['srv:sqlGenTable:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['srv:sqlGenTable:export']">导出</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['tool:gen2:import']" icon="Upload" plain type="info" @click="openImportTable">导入
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="sqlGenTableList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" v-if="true" />
        <el-table-column label="编号" align="center" prop="tableId" />
        <el-table-column label="数据源名称" align="center" prop="dataName" />
        <el-table-column label="表名称" align="center" prop="tableName" />
        <el-table-column label="表描述" align="center" prop="tableComment" />
        <el-table-column label="关联子表的表名" align="center" prop="subTableName" />
        <el-table-column label="子表关联的外键名" align="center" prop="subTableFkName" />
        <el-table-column label="实体类名称" align="center" prop="className" />
        <el-table-column label="使用的模板" align="center" prop="tplCategory" />
        <el-table-column label="生成包路径" align="center" prop="packageName" />
        <el-table-column label="生成模块名" align="center" prop="moduleName" />
        <el-table-column label="生成业务名" align="center" prop="businessName" />
        <el-table-column label="生成功能名" align="center" prop="functionName" />
        <el-table-column label="生成功能作者" align="center" prop="functionAuthor" />
        <el-table-column label="生成代码方式" align="center" prop="genType" />
        <el-table-column label="生成代码方式" align="center" prop="sqlType" />
        <el-table-column label="业务名称" align="center" prop="sqlName" />
        <el-table-column label="生成路径" align="center" prop="genPath" />
        <el-table-column label="其它生成选项" align="center" prop="options" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['srv:sqlGenTable:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['srv:sqlGenTable:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改代码生成业务2sql对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="sqlGenTableFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="tableId">
          <el-input v-model="form.tableId" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="数据源名称" prop="dataName">
          <el-input v-model="form.dataName" placeholder="请输入数据源名称" />
        </el-form-item>
        <el-form-item label="表名称" prop="tableName">
          <el-input v-model="form.tableName" placeholder="请输入表名称" />
        </el-form-item>
        <el-form-item label="表描述" prop="tableComment">
            <el-input v-model="form.tableComment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="关联子表的表名" prop="subTableName">
          <el-input v-model="form.subTableName" placeholder="请输入关联子表的表名" />
        </el-form-item>
        <el-form-item label="子表关联的外键名" prop="subTableFkName">
          <el-input v-model="form.subTableFkName" placeholder="请输入子表关联的外键名" />
        </el-form-item>
        <el-form-item label="实体类名称" prop="className">
          <el-input v-model="form.className" placeholder="请输入实体类名称" />
        </el-form-item>
        <el-form-item label="使用的模板" prop="tplCategory">
          <el-input v-model="form.tplCategory" placeholder="请输入使用的模板" />
        </el-form-item>
        <el-form-item label="生成包路径" prop="packageName">
          <el-input v-model="form.packageName" placeholder="请输入生成包路径" />
        </el-form-item>
        <el-form-item label="生成模块名" prop="moduleName">
          <el-input v-model="form.moduleName" placeholder="请输入生成模块名" />
        </el-form-item>
        <el-form-item label="生成业务名" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入生成业务名" />
        </el-form-item>
        <el-form-item label="生成功能名" prop="functionName">
          <el-input v-model="form.functionName" placeholder="请输入生成功能名" />
        </el-form-item>
        <el-form-item label="生成功能作者" prop="functionAuthor">
          <el-input v-model="form.functionAuthor" placeholder="请输入生成功能作者" />
        </el-form-item>
        <el-form-item label="业务名称" prop="sqlName">
          <el-input v-model="form.sqlName" placeholder="请输入业务名称" />
        </el-form-item>
        <el-form-item label="生成路径" prop="genPath">
          <el-input v-model="form.genPath" placeholder="请输入生成路径" />
        </el-form-item>
        <el-form-item label="其它生成选项" prop="options">
            <el-input v-model="form.options" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <import-table ref="importRef" @ok="handleQuery"/>
  </div>
</template>

<script setup name="SqlGenTable" lang="ts">
import {
  addSqlGenTable,
  delSqlGenTable,
  getSqlGenTable,
  listSqlGenTable,
  updateSqlGenTable
} from '@/api/srv/sqlGenTable';
import {SqlGenTableForm, SqlGenTableQuery, SqlGenTableVO} from '@/api/srv/sqlGenTable/types';
import ImportTable from "./importTable.vue";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const sqlGenTableList = ref<SqlGenTableVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const sqlGenTableFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: SqlGenTableForm = {
  tableId: undefined,
  dataName: undefined,
  tableName: undefined,
  tableComment: undefined,
  subTableName: undefined,
  subTableFkName: undefined,
  className: undefined,
  tplCategory: undefined,
  packageName: undefined,
  moduleName: undefined,
  businessName: undefined,
  functionName: undefined,
  functionAuthor: undefined,
  genType: undefined,
  sqlType: undefined,
  sqlName: undefined,
  genPath: undefined,
  options: undefined,
  remark: undefined
}
const data = reactive<PageData<SqlGenTableForm, SqlGenTableQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: undefined,
    tableId: undefined,
    dataName: undefined,
    tableName: undefined,
    tableComment: undefined,
    subTableName: undefined,
    subTableFkName: undefined,
    className: undefined,
    tplCategory: undefined,
    packageName: undefined,
    moduleName: undefined,
    businessName: undefined,
    functionName: undefined,
    functionAuthor: undefined,
    genType: undefined,
    sqlType: undefined,
    sqlName: undefined,
    genPath: undefined,
    options: undefined,
    params: {
    }
  },
  rules: {
    tableId: [
      { required: true, message: "编号不能为空", trigger: "blur" }
    ],
    dataName: [
      { required: true, message: "数据源名称不能为空", trigger: "blur" }
    ],
    tableName: [
      { required: true, message: "表名称不能为空", trigger: "blur" }
    ],
    tableComment: [
      { required: true, message: "表描述不能为空", trigger: "blur" }
    ],
    subTableName: [
      { required: true, message: "关联子表的表名不能为空", trigger: "blur" }
    ],
    subTableFkName: [
      { required: true, message: "子表关联的外键名不能为空", trigger: "blur" }
    ],
    className: [
      { required: true, message: "实体类名称不能为空", trigger: "blur" }
    ],
    tplCategory: [
      { required: true, message: "使用的模板不能为空", trigger: "blur" }
    ],
    packageName: [
      { required: true, message: "生成包路径不能为空", trigger: "blur" }
    ],
    moduleName: [
      { required: true, message: "生成模块名不能为空", trigger: "blur" }
    ],
    businessName: [
      { required: true, message: "生成业务名不能为空", trigger: "blur" }
    ],
    functionName: [
      { required: true, message: "生成功能名不能为空", trigger: "blur" }
    ],
    functionAuthor: [
      { required: true, message: "生成功能作者不能为空", trigger: "blur" }
    ],
    genType: [
      { required: true, message: "生成代码方式不能为空", trigger: "change" }
    ],
    sqlType: [
      { required: true, message: "生成代码方式不能为空", trigger: "change" }
    ],
    sqlName: [
      { required: true, message: "业务名称不能为空", trigger: "blur" }
    ],
    genPath: [
      { required: true, message: "生成路径不能为空", trigger: "blur" }
    ],
    options: [
      { required: true, message: "其它生成选项不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询代码生成业务2sql列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSqlGenTable(queryParams.value);
  sqlGenTableList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  sqlGenTableFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: SqlGenTableVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加代码生成业务2sql";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: SqlGenTableVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getSqlGenTable(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改代码生成业务2sql";
}

/** 提交按钮 */
const submitForm = () => {
  sqlGenTableFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateSqlGenTable(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addSqlGenTable(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: SqlGenTableVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除代码生成业务2sql编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delSqlGenTable(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('srv/sqlGenTable/export', {
    ...queryParams.value
  }, `sqlGenTable_${new Date().getTime()}.xlsx`)
}

const importRef = ref<InstanceType<typeof ImportTable>>();

/** 打开导入表弹窗 */
const openImportTable = () => {
  importRef.value?.getList(queryParams.value.dataName);
}

onMounted(() => {
  getList();
});
</script>
